﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;
using System.Dynamic;
using FluorineFx.Json;

namespace lxwmvc
{
    /// <summary>
    /// 提现复核
    /// </summary>
    public static partial class huifucontroller
    {
        /// <summary>
        /// 内部调用
        /// </summary>
        /// <param name="objrev"></param>
        static void huifu_tixianfuhe(RetCash objrev)
        {
            //插入到提现复核流水表中
            string User_Id = core_user.getUserIdByUsrCustId(objrev.UsrCustId);

            supermodel cashaudit = supermodel.create("Mcashaudit");
            cashaudit["User_Id"] = User_Id;
            cashaudit["MerCustId"] = objrev.MerCustId + "";
            cashaudit["UsrCustId"] = objrev.UsrCustId + "";
            cashaudit["OrdId"] = objrev.OrdId + "";
            //R代表拒绝 S表示复核通过 
            cashaudit["AuditFlag"] = "R";
            cashaudit["Type"] = "CashAudit";
            cashaudit["TransAmt"] = objrev.TransAmt;
            string Mcashauditid = supermodel.create("Mcashaudit")
                .select("Mcashauditid")
                .where(" OrdId='" + objrev.OrdId + "' and UsrCustId='" + objrev.UsrCustId + "'")
                .one<string>();
                
            //判断是否存在复核流水
            if (Mcashauditid == "")
            {
                var ret = cashaudit.saveorupdate();
                Mcashauditid = ret.identity + "";
            }

            //提现复核
            CashAudit objsend = new CashAudit();
            objsend.UsrCustId = objrev.UsrCustId + "";
            objsend.OrdId = objrev.OrdId + "";
            objsend.AuditFlag = "S";
            objsend.MerPriv = Mcashauditid;
            objsend.TransAmt = objrev.TransAmt + "";
            objsend.BgRetUrl = HuiFuArg.LocalUrl + "huifu/huifu_tixianfuhe_returnbg";
            var tobjrevcash = core_huifu.Send<RetCashAudit>(objsend);
            {
                core_huifu.saveHuiFuLiuShui(tobjrevcash);
                if (tobjrevcash.RespCode == core_huifu.HUIFU_SUCCESS)
                {
                    huifu_tixianfuhe_fun(objrev.OrdId, objrev.UsrCustId, objrev.TransAmt, User_Id, tobjrevcash);
                }
            }
        }

        /// <summary>
        /// 提现复核通用执行方法
        /// </summary>
        /// <param name="OrdId"></param>
        /// <param name="UsrCustId"></param>
        /// <param name="TransAmt"></param>
        /// <param name="User_Id"></param>
        /// <param name="tobjrevcash"></param>
        static void huifu_tixianfuhe_fun(string OrdId, string UsrCustId,string TransAmt, string User_Id, RetCashAudit tobjrevcash)
        {
            //更新记录状态
            supermodel.create("Mcashaudit")
                .set("AuditFlag", "S")//通过
                .set("FeeAmt", tobjrevcash.FeeAmt)
                .set("FeeCustId", tobjrevcash.FeeCustId)//客户号
                .set("FeeAcctId", tobjrevcash.FeeAcctId)//客户子账户
                .where("Mcashauditid=" + tobjrevcash.MerPriv)
                .update();

            //删除多余生成记录（该订单可能产生多余的记录，然后删除掉，其实好像没有什么用处？？）
            supermodel.create("Mcashaudit")
                .where("OrdId='" + OrdId + "' and UsrCustId='" + UsrCustId + "' and AuditFlag='R'")
                .delete();

            //发送短信
            string MOBIL_NO = core_user.getMobileByUsrCustId(UsrCustId);
            core_sms.SendMsg(MOBIL_NO, "亲爱的嘉金投资用户,您于" + DateTime.Now.ToString("F") + "成功提现" + TransAmt.tostr() + "元整。若非本人操作，请您联系致电客服。400-100-0616");
            //发送站内信
            core_user.sendMessage(User_Id, "亲爱的嘉金投资用户,您于" + DateTime.Now.ToString("F") + "成功提现" + TransAmt.tostr() + "元整。若非本人操作，请您联系致电客服。400-100-0616");
        }

        /// <summary>
        /// 复核提现，后台调用此方法
        /// </summary>
        /// <param name="pb"></param>
        public static void huifu_tixianfuhe_out(pagebase pb )
        {
            int Mdrawmoneyid = pb.v_i("Mdrawmoneyid");
            //Mdrawmoneyid = cryptohelper.decode(Mdrawmoneyid);
            supermodel Mdrawmoney = supermodel.create("Mdrawmoney")
                .where("Mdrawmoneyid=" + Mdrawmoneyid)
                .get();
            
            //解冻金额  
            huifu_jiedongjine(Mdrawmoney["TrxId"].tostr());


            string User_Id = core_user.getUserIdByUsrCustId(Mdrawmoney["UsrCustId"] + "");

            //插入到提现复核流水表中
            supermodel cashaudit = supermodel.create("Mcashaudit");
            cashaudit["User_Id"] = User_Id;
            cashaudit["MerCustId"] = Mdrawmoney["MerCustId"] + "";
            cashaudit["UsrCustId"] = Mdrawmoney["UsrCustId"] + "";
            cashaudit["OrdId"] = Mdrawmoney["OrdId"] + "";
            cashaudit["AuditFlag"] = "R";
            cashaudit["Type"] = "CashAudit";
            cashaudit["TransAmt"] = Mdrawmoney["TransAmt"];

            //复核申请的编号
            string Mcashauditid = supermodel.create("Mcashaudit")
                .select("Mcashauditid")
                .where(" OrdId='" + Mdrawmoney["OrdId"] + "" + "' and UsrCustId='" + Mdrawmoney["UsrCustId"] + "" + "'")
                .one<string>();

            //防止重复插入
            if (Mcashauditid == "")
            {
                var ret = cashaudit.saveorupdate();
                Mcashauditid = ret.identity + "";
            }

            //提现复核
            CashAudit objsend = new CashAudit();
            objsend.UsrCustId = Mdrawmoney["UsrCustId"] + "";
            objsend.OrdId = Mdrawmoney["OrdId"] + "";
            //通过
            objsend.AuditFlag = "S";
            objsend.MerPriv = Mcashauditid;
            objsend.TransAmt = Mdrawmoney["TransAmt"] + "";
            objsend.BgRetUrl = HuiFuArg.LocalUrl + "huifu/huifu_tixianfuhe_returnbg";
            var tobjrevcash = core_huifu.Send<RetCashAudit>(objsend);
            {
                core_huifu.saveHuiFuLiuShui(tobjrevcash);
                if (tobjrevcash.RespCode == core_huifu.HUIFU_SUCCESS)
                {
                    huifu_tixianfuhe_fun(objsend.OrdId, objsend.UsrCustId, objsend.TransAmt, User_Id, tobjrevcash);
                    return;
                }
                else
                {
                    //jshelper.alertandclose("提现复核失败" + tobjrevcash.RespCode + "！");
                    return;
                }
            }
        }

        /// <summary>
        /// 汇付返回调用参数
        /// </summary>
        /// <param name="pb"></param>
        public static void huifu_tixianfuhe_returnbg(pagebase pb)
        {
            RetCashAudit objrev = new RetCashAudit();
            //后台调用，最终要打印出来参数
            core_huifu.ReturnUrl(HttpContext.Current, "OrdId", objrev, true);
        }
    }
}